<?php
/**
 * Created by PhpStorm.
 * User: 96572
 * Date: 2017/4/9
 * Time: 20:21
 */

namespace app\modules\admin\models;

use Yii;
use yii\db\ActiveRecord;
use yii\widgets\ActiveForm;

class RBAC extends ActiveRecord
{
    public $module_id;
    public $controller_id;
    public $action_id;
    public $description;
    public static function tableName()
    {
        return '{{%auth_item}}';
    }
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [
                ['module_id', 'controller_id', 'action_id','description'],
                'required',
                'message'   =>  Yii::t('blog','NOT_BLANK')
            ],
            [
                ['module_id', 'controller_id', 'action_id'],
                'validateActionID'
            ],
            ['module_id','validateModuleID'],
            ['controller_id','validateControllerID'],
            ['action_id','validateActionID'],
            // [['user_name'], 'unique'],
        ];
    }
    public function attributeLabels()
    {
        return [
            'module_id' => '模块id',
            'controller_id' => '控制器id',
            'action_id' => '动作id',
            'description' => '节点描述',
        ];
    }

    /**
     * 验证模块是否存在
     * @return bool
     */
    public function validateModuleID()
    {
        if(!\Yii::$app->hasModule($this->module_id))
        {
            $this->addError(
                'module_id',
                \Yii::t('blog','RBAC_MODULE_NOT_EXIST')
            );
            return false;
        }
        return true;
    }

    /**
     * 验证控制器是否存在
     * @return bool
     */
    public function validateControllerID()
    {
        if(false === $this->validateModuleID())
        {
            return false;
        }

        $module = \Yii::$app->getModule($this->module_id);
        $controller = $module->createControllerByID($this->controller_id);
        if(!$controller)
        {
            $this->addError(
                'controller_id',
                \Yii::t('blog','RBAC_CONTROLLER_NOT_EXIST')
            );
            return false;
        }
        return true;
    }

    /**
     * 验证action是否存在
     * @return bool
     */
    public function validateActionID()
    {
        if (false === $this->validateControllerID())
        {
            return false;
        }

        $module = \Yii::$app->getModule($this->module_id);
        $controller = $module->createControllerByID($this->controller_id);
        $action = $controller->createAction($this->action_id);
        if(!$action)
        {
            $this->addError('action_id',\Yii::t('blog','RBAC_ACTION_NOT_EXIST'));
            return false;
        }

        if(AuthItem::find()->where(['name'=>static::getRbacID($this->module_id,$this->controller_id,$this->action_id)])->select('name')->exists())
        {
            $this->addError('module_id',\Yii::t('blog','PERMISSION_EXISTS'));
            $this->addError('controller_id',\Yii::t('blog','PERMISSION_EXISTS'));
            $this->addError('action_id',\Yii::t('blog','PERMISSION_EXISTS'));
            return false;
        }
        return true;
    }

    public function save($runValidation = true, $attributeNames = null)
    {
        $this->addErrors(ActiveForm::validate($this));
        if($this->errors)
        {
            return false;
        }
        $auth = Yii::$app->authManager;
        $name = static::getRbacID(
            $this->module_id,
            $this->controller_id,
            $this->action_id
        );
        $createPost = $auth->createPermission($name);
        $createPost->description = $this->description;
        return $auth->add($createPost);
    }
}